//
//  main.m
//  05-CollectionTraversalHomeWork
//
//  Created by 曹曹 on 16/7/8.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
#import "Province.h"
#import "City.h"
#import "Town.h"
#import "TempCity.h"

void homeWork1 () {
    // 1.创建1个包含5个字符串元素的数组；
    // 2.用for循环倒序输出所有元素；
    // 3.用for...in完成所有字符串元素的拼接(生成一个新的字符串)，并输出拼接好的字符串。
    NSArray *stringArray = @[@"Hello ", @"World! ", @"Welcome ", @"To ", @"Dalian!"];
    for (NSInteger i = stringArray.count - 1; i >= 0; i--) {
        NSLog(@"%@", stringArray[i]);
    }
    NSMutableString *appendString = [NSMutableString string];
    for (NSString *string in stringArray) {
        [appendString appendString:string];
    }
    NSLog(@"%@", appendString);
}
void homeWork2 () {
    // 1.创建1个包含5个person对象的数组(姓名用英文)；
    // 2.用for循环找到年龄最大的Person对象；
    // 3.用for...in遍历数组，把大于某个年龄的Person对象的姓名后面拼接上字符串“-brother”。
    Person *per1 = [[Person alloc] initWithName:@"zhangsan" age:23 gender:@"male"];
    Person *per2 = [[Person alloc] initWithName:@"lisiguan" age:24 gender:@"female"];
    Person *per3 = [[Person alloc] initWithName:@"wangwang" age:30 gender:@"female"];
    Person *per4 = [[Person alloc] initWithName:@"alibabab" age:33 gender:@"male"];
    Person *per5 = [[Person alloc] initWithName:@"baidulal" age:40 gender:@"male"];
    NSArray *personArray = @[per1, per2, per3, per4, per5];
    NSUInteger temp = 0;
    for (NSInteger i = 0; i < personArray.count; i++) {
        Person *person = personArray[i];
        if (person.age > temp) {
            temp = person.age;
        }
    }
    NSLog(@"年龄最大的是%ld岁!", temp);
    for (Person *person in personArray) {
        if (person.age > 30) {
            person.name = [person.name stringByAppendingString:@"-brother"];
        }
        NSLog(@"%@", person.name);
    }
    // 1.使用第二题目中生成的数组；
    // 2.使用sortUsingSelector: 分别按年龄，姓名，性别进行排序，输出排序结果；
    // 3.使用NSSortDescriptor，按姓名进行排序。
    NSArray *sortByName = [personArray sortedArrayUsingSelector:@selector(compareByName:)];
    NSArray *sortByAge = [personArray sortedArrayUsingSelector:@selector(compareByAge:)];
    NSArray *sortByGender = [personArray sortedArrayUsingSelector:@selector(compareByGender:)];
    NSLog(@"%@", sortByName);
    NSLog(@"%@", sortByAge);
    NSLog(@"%@", sortByGender);
    NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    personArray = [personArray sortedArrayUsingDescriptors:@[nameDescriptor]];
    NSLog(@"%@", personArray);
}
void homeWork3 () {
    // 处理省市区文件中的area.txt
    // 将省市区进行分割处理, 省里包含市, 市里包含区
    // 1. 先用数组字典表示
    // 2. 将数组字典的处理方式转换成数组模型(Class)
    NSString *sourceData = [NSString stringWithContentsOfFile:@"/Users/caocao/DLS160710/Objective-C/HomeWork/05-CollectionTraversalHomeWork/05-CollectionTraversalHomeWork/area.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *data = [sourceData componentsSeparatedByString:@"\n"];
    NSMutableArray *towns = [NSMutableArray array];
    NSMutableDictionary *city = [NSMutableDictionary dictionary];
    NSMutableDictionary *province = [NSMutableDictionary dictionary];
    int provIndex = 0;
    int cityIndex = 0;
    for (int i = 0; i < data.count; i++) {
        if (![[data[i] substringToIndex:1] containsString:@" "]) {
            provIndex = i;
        } else if (![[data[i] substringWithRange:NSMakeRange(2, 1)] isEqualToString:@" "]) {
            cityIndex = i;
        } else {
            if ([province objectForKey:data[provIndex]] == nil) {
                city = [NSMutableDictionary dictionary];
                towns = [NSMutableArray arrayWithObject:data[i]];
                [city setObject:towns forKey:data[cityIndex]];
                [province setObject:city forKey:data[provIndex]];
            } else {
                if ([city objectForKey:data[cityIndex]] == nil) {
                    towns = [NSMutableArray arrayWithObject:data[i]];
                    [city setObject:towns forKey:data[cityIndex]];
                } else {
                    [towns addObject:data[i]];
                }
            }
        }
    }
    NSLog(@"%@", province);
}
void province_city_town () {
    NSString *sourceData = [NSString stringWithContentsOfFile:@"/Users/caocao/DLS160710/Objective-C/HomeWork/05-CollectionTraversalHomeWork/05-CollectionTraversalHomeWork/area.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *data = [sourceData componentsSeparatedByString:@"\n"];
#pragma mark - 思路 1 省数组 - 省字典 - 市数组 - 市字典 - 区数组 - 区字典
    NSMutableArray *allProvinceArray = [NSMutableArray array];
    for (NSString *area in data) {
        // 取到所有的省
        if (![area hasPrefix:@" "]) {
            // 创建了省字典(字典中包含两个字段 proName cityArray)
            NSMutableDictionary *proDic = [NSMutableDictionary dictionary];
            // 添加ProName字段
            [proDic setObject:area forKey:@"proName"];
            // 创建一个市数组(省对应的所有市)
            NSMutableArray *cityArray = [NSMutableArray array];
            // 添加数组字段
            [proDic setObject:cityArray forKey:@"cityArray"];
            // 将创建好的省字典, 放到省数组中
            [allProvinceArray addObject:proDic];
            // 取到所有的市(两个空格开头, 但不是四个空格开头)
        } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
            // 难点:通过lastobject获取到对应的省字典!!!!!!!!!!!!!!!!!!!!!!!
            NSMutableDictionary *proDic = [allProvinceArray lastObject];
            // 拿到省字典中对应的市数组
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            // 创建市字典(cityName, townArray)
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
            [cityDic setObject:area forKey:@"cityName"];
            NSMutableArray *townArray = [NSMutableArray array];
            [cityDic setObject:townArray forKey:@"townArray"];
            // 向市数组中添加市字典
            [cityArray addObject:cityDic];
        } else {
            // 通过省数组获取到当前所在省的字典
            NSMutableDictionary *proDic = [allProvinceArray lastObject];
            // 通过省字典获取到市数组
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            // 通过市数组获取到对应的市字典
            NSMutableDictionary *city = [cityArray lastObject];
            // 通过市字典获取到对应的区数组
            NSMutableArray *townArray = [city objectForKey:@"townArray"];
            // 将区添加到数组中
            [townArray addObject:area];
        }
    }
//    NSLog(@"%@", allProvinceArray);
#pragma mark - 思路 2 Province类
    NSMutableArray *provinceObjectArray = [NSMutableArray array];
    for (NSString *area in data) {
        if (![area hasPrefix:@" "]) {
            Province *prov = [Province townWithName:area];
            [provinceObjectArray addObject:prov];
        } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
            Province *prov = [provinceObjectArray lastObject];
            City *city = [City townWithName:area];
            [prov.array addObject:city];
        } else {
            City *city = [[[provinceObjectArray lastObject] array] lastObject];
            Town *town = [Town townWithName:area];
            [city.array addObject:town];
        }
    }
    NSLog(@"%@", provinceObjectArray);
}
void homeWork4 () {
    NSString *sourceData = [NSString stringWithContentsOfFile:@"/Users/caocao/DLS160710/Objective-C/HomeWork/05-CollectionTraversalHomeWork/05-CollectionTraversalHomeWork/Location1.txt" encoding:NSUTF8StringEncoding error:nil];
    // 根据","分割字符串, 得到所有城市的数组
    NSArray *allCityArray = [sourceData componentsSeparatedByString:@","];
    NSMutableArray *allCityDicArray = [NSMutableArray array];
    // 根据每个城市中的"|"处理字符串, 重新生成字典的数组
    for (NSString *city in allCityArray) {
        // 根据"|"切割字符串
        NSArray *arrayCity = [city componentsSeparatedByString:@"|"];
        // 根据切割生成的数组, 生成新的城市字典
        NSMutableDictionary *cityDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[arrayCity firstObject], @"cityName", [arrayCity lastObject], @"cityNumber", nil];
        [allCityDicArray addObject:cityDic];
    }
#pragma mark - 排序(重要)
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
    [allCityDicArray sortUsingDescriptors:@[sort]];
    NSMutableArray *finalArray = [NSMutableArray array];
    for (NSMutableDictionary *cityDic in allCityDicArray) {
        NSString *cityNumber = cityDic[@"cityNumber"];
        if ([cityNumber hasSuffix:@"0000"]) {
            NSMutableDictionary *provDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [provDic setObject:[NSMutableArray array] forKey:@"cityArray"];
            [finalArray addObject:provDic];
        } else if ([cityNumber hasSuffix:@"00"]) {
            NSMutableDictionary *provDic = [finalArray lastObject];
            NSMutableArray *cityArray = [provDic objectForKey:@"cityArray"];
            NSMutableDictionary *dicCity = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [dicCity setObject:[NSMutableArray array] forKey:@"townArray"];
            [cityArray addObject:dicCity];
        } else {
//            NSMutableArray *townArray = [[[[finalArray lastObject] objectForKey:@"cityArray"] lastObject] objectForKey:@"townArray"];
#pragma mark - 写代码尽量分开写
            NSMutableDictionary *provDic = [finalArray lastObject];
            NSMutableArray *cityArray = [provDic objectForKey:@"cityArray"];
            NSMutableDictionary *dicCity = [cityArray lastObject];
            NSMutableArray *townArray = [dicCity objectForKey:@"townArray"];
            NSMutableDictionary *townDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [townArray addObject:townDic];
        }
    }
//    NSLog(@"%@", finalArray);
#pragma mark - 转换成类
    NSMutableArray *allTempCityArray = [NSMutableArray array];
    for (NSString *cityString in allCityArray) {
        NSArray *city = [cityString componentsSeparatedByString:@"|"];
        TempCity *tempCity = [[TempCity alloc] initWithName:[city firstObject] number:[city lastObject]];
        [allTempCityArray addObject:tempCity];
    }
//    NSLog(@"%@", allTempCityArray);
    NSSortDescriptor *sortTempCity = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
    [allTempCityArray sortUsingDescriptors:@[sortTempCity]];
    NSMutableArray *finalClassArray = [NSMutableArray array];
    for (TempCity *tempCity in allTempCityArray) {
        if ([tempCity.cityNumber hasSuffix:@"0000"]) {
            Province *prov = [Province townWithTempCity:tempCity];
            [finalClassArray addObject:prov];
        } else if ([tempCity.cityNumber hasSuffix:@"00"]) {
            Province *prov = [finalClassArray lastObject];
            City *city = [City townWithTempCity:tempCity];
            [prov.array addObject:city];
        } else {
            Province *prov = [finalClassArray lastObject];
            City *city = [prov.array lastObject];
            Town *town = [Town townWithTempCity:tempCity];
            [city.array addObject:town];
        }
    }
//    NSLog(@"%@", finalClassArray);
}

int main(int argc, const char * argv[]) {
//    homeWork1();
//    homeWork2();
//    homeWork3();
//    province_city_town();
    homeWork4();
    return 0;
}
